package com.lhc.service.impl;

import com.lhc.dao.UserDOMapper;
import com.lhc.dao.UserPasswordDOMapper;
import com.lhc.dataObject.UserDO;
import com.lhc.dataObject.UserPasswordDO;
import com.lhc.error.BusniessException;
import com.lhc.error.EmBussinessError;
import com.lhc.service.UserService;
import com.lhc.service.model.UserModel;
import com.lhc.validator.ValidationImpl;
import com.lhc.validator.ValidationResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDOMapper userDOMapper;

    @Autowired
    private UserPasswordDOMapper userPasswordDOMapper;

    @Autowired
    private ValidationImpl validation;

    @Override
    public UserModel selectByPrimaryKey(Integer id) {
        UserDO userDO = userDOMapper.selectByPrimaryKey(id);
        if (userDO == null) {
            return null;
        }
        UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(id);
        UserModel userModel = convertFromDataObject(userDO, userPasswordDO);
        return userModel;
    }

    //注册业务
    @Override
    @Transactional
    public void register(UserModel userModel) throws BusniessException {
        if (userModel == null) {
            throw new BusniessException(EmBussinessError.PARAMETER_VALIDATION_ERROR);
        }
        //校验
        ValidationResult result = validation.validate(userModel);
        if (result.isHasErrors()){
            throw new BusniessException(EmBussinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
        }

        UserDO userDO = convertFromUserModel(userModel);

        try {
            userDOMapper.insertSelective(userDO);
        } catch (DuplicateKeyException e) {
            throw new BusniessException(EmBussinessError.PARAMETER_VALIDATION_ERROR, "手机号重复");
        }

        userModel.setId(userDO.getId());
        UserPasswordDO userPasswordDO = convertPasswordFromUserModel(userModel);
        userPasswordDOMapper.insertSelective(userPasswordDO);
    }

    //登录验证
    @Override
    public UserModel validateLogin(String telphone, String encrptPassword) throws BusniessException {
        //通过手机号查询用户信息
        UserDO userDO = userDOMapper.selectByTelphone(telphone);
        if (userDO == null) {
            //抛出异常用户不存在
            throw new BusniessException(EmBussinessError.USER_NOT_EXIST);
        }
        //通过id获取密码对象
        UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());

        UserModel userModel = convertFromDataObject(userDO, userPasswordDO);
        //对比前台输入的密码和查询的用户密码
        if (!StringUtils.equals(userModel.getEncrptPassword(), encrptPassword)) {
            throw new BusniessException(EmBussinessError.USER_VALIDATE_ERROR);
        }
        return userModel;

    }


    //从Model对象中获取密码对象
    private UserPasswordDO convertPasswordFromUserModel(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        UserPasswordDO userPasswordDO = new UserPasswordDO();
        userPasswordDO.setEncrptPassword(userModel.getEncrptPassword());
        userPasswordDO.setUserId(userModel.getId());
        return userPasswordDO;
    }

    //从Model对象中获取do对象
    private UserDO convertFromUserModel(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userModel, userDO);
        return userDO;
    }

    //从do对象和passworddo对象中获取model对象
    private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO) {
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(userDO, userModel);
        if (userPasswordDO != null)
            userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
        return userModel;

    }

}
